package com.ihg.hiex.admin.service.session.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.commons.collections.MapUtils;
import org.springframework.stereotype.Service;

import com.demo2do.core.persistence.annotation.Dao;
import com.demo2do.core.utils.CollectionUtils;
import com.ihg.hiex.admin.dao.account.KnowingRelationDao;
import com.ihg.hiex.admin.entity.session.RelationShip;
import com.ihg.hiex.admin.service.DataServiceImpl;
import com.ihg.hiex.admin.service.session.KnowingRelationService;
import com.ihg.hiex.entity.session.Session;
import com.ihg.hiex.entity.user.Assessor;
import com.ihg.hiex.entity.user.Delegate;
import com.ihg.hiex.entity.user.KnowingLevel;
import com.ihg.hiex.entity.user.KnowingRelation;

/**
 *
 * @author Ark
 *
 */
@Service("knowingRelationService")
public class KnowingRelationServiceImpl extends DataServiceImpl<KnowingRelation> implements KnowingRelationService {

    @Dao
    private KnowingRelationDao knowingRelationDao;
    
    /* (non-Javadoc)
     * @see com.ihg.hiex.admin.service.session.KnowingRelationService#list(com.ihg.hiex.entity.session.Session)
     */
    public List<KnowingRelation> list(Session session) {
        return knowingRelationDao.list(session);
    }
    
    /* (non-Javadoc)
     * @see com.ihg.hiex.admin.service.session.KnowingRelationService#list(com.ihg.hiex.entity.session.Session, com.ihg.hiex.entity.user.Assessor, com.ihg.hiex.entity.user.Delegate)
     */
    public KnowingRelation load(Session session, Assessor assessor, Delegate delegate) {
        return knowingRelationDao.load(session, assessor, delegate);
    }
    
    /*
     * (non-Javadoc)
     * @see com.ihg.hiex.admin.service.session.KnowingRelationService#listRelationShip(com.ihg.hiex.entity.session.Session, java.util.List, java.util.List)
     */
    public List<RelationShip> listRelationShip(Session session, List<Assessor> assessors, List<Delegate> delegates) {
        
        // 1. list knowingRelations map->key:assessorId
        List<KnowingRelation> relations = knowingRelationDao.list(session);
        Map<String, List<KnowingRelation>> knowingRelations = CollectionUtils.constructMapValues(relations, "assessor.id");
        
        // 2. Map -> key:assessorId; value->key:delegateId, value:knowingRelation
        Map<String, Map<String, KnowingRelation>> result = new HashMap<String, Map<String, KnowingRelation>>();
        for(Entry<String, List<KnowingRelation>> knowingRelation : knowingRelations.entrySet()) {
            Map<String, KnowingRelation> relation = CollectionUtils.constructMapValue(knowingRelation.getValue(), "delegate.id");
            result.put(knowingRelation.getKey(), relation);
        }
        
        // 3. list relationShip by delegate and assessor
        List<RelationShip> relationShips = new ArrayList<RelationShip>();
        for(Delegate delegate : delegates) {
            List<KnowingLevel> levels = new ArrayList<KnowingLevel>();
            for(Assessor assessor : assessors) {
                if(MapUtils.isNotEmpty(result) && 
                   result.get(assessor.getId().toString()) != null && 
                   result.get(assessor.getId().toString()).get(delegate.getId().toString()) != null) {
                    KnowingRelation relation = result.get(assessor.getId().toString()).get(delegate.getId().toString());
                    levels.add(relation.getKnowingLevel());
                } else {
                    levels.add(null);
                }
            }
            RelationShip relationShip = new RelationShip(delegate.getName(), levels);
            relationShips.add(relationShip);
        }
        
        return relationShips;
        
    }
    
    /* (non-Javadoc)
     * @see com.ihg.hiex.admin.service.session.KnowingRelationService#listAssessor(com.ihg.hiex.entity.session.Session)
     */
    public List<Assessor> listAssessor(Session session) {
        return knowingRelationDao.listAssessor(session);
    }
    
    /* (non-Javadoc)
     * @see com.ihg.hiex.admin.service.session.KnowingRelationService#listDelegate(com.ihg.hiex.entity.session.Session)
     */
    public List<Delegate> listDelegate(Session session) {
        return knowingRelationDao.listDelegate(session);
    }
    
}
